<?php

class ServicesController extends AppController {
  public $helpers = array('Html', 'TWSForm', 'Paginator');
  public $components = array('Session', 'Util', 'Upload');
  public $name = 'Services';
  public $paginate;


  function __construct($request=null, $response=null) {
    parent::__construct($request, $response);
    $this->paginate = array(
        'limit' => Configure::read('App.pageResultLimit'),
        'order' => array(
            'Service.name' => 'asc'
            ));
  }

  function view() {
    $this->layout = 'ad';
    $this->set("title_for_layout","Weddings in Trinidad and Tobago");
    $this->set("meta_keywords","Isle Vow Services Products Trinidad Tobago Wedding Services");
    $this->set("meta_description","An outline of the services that are offered at Isle Vow to make your wedding in Trinidad and Tobago a dream come true.");
    $this->set('services', $this->paginate('Service'));
  }

  function companies ($serviceId=null) {
    if(empty($serviceId)){
      throw new NotFoundException();
    } else {
      $this->loadModel('Company');
    }
  }

    function details() {
    $this->set("title_for_layout","Services Details at Isle Vow");
    $this->set("meta_keywords","Isle Vow Service Details");
    $this->set("meta_description","More detailed information on services that are offered by Isle Vow");
  }

   function products($id=null) {
    $this->set("title_for_layout","Products and Special Offers at Isle Vow");
    $this->set("meta_keywords","Isle Vow Products Specials Offers Trinidad and Tobago");
    $this->set("meta_description","Details on products and services offerred at Isle Vow");
  }

  function menu () {
    $services = $this->Session->read('Services');
    if (empty($services)) {
      $services = $this->Service->find("all", array("order" => array("Service.name" => 'asc')));
      $this->Session->write('Services', $services);
    }
    return $services;
  }

    //admin

  function admin_index() {
    $this->layout="admin";
    $this->set('services', $this->paginate('Service'));
  }

  function admin_add() {
    $this->layout = "admin";
    $test = $this->Service->validate;
    if ($this->request->is('post')) {
      $this->_processAction($this->request, 'add');
    }
  }
    //admin
  function admin_edit($id = null) {
    $this->layout="admin";
    $this->Service->id = $id;
    if ($this->request->is('get')) {
      $this->request->data = $this->Service->read();
    } else {
      //check for delete announcement
      if (isset($this->request->data['deleteBtn'])) {
        $this->admin_delete($id);
      } else {
        $this->_processAction($this->request, 'edit', $id);
      }
    }
  }

    private function _processAction($dataIn, $action='add', $id=null) {
      $data = $this->_processRequestData($dataIn);
      $this->Service->set($data);
      if ($this->Service->validates()) {
        if (!empty($data["Service"]["image_path"]) && !empty($data["Service"]["image_path"]['name'])) {
          $imageUploaded = $this->Upload->uploadImage($data["Service"]["image_path"], WWW_ROOT . '/' . Configure::read('App.imageFolder') . '/' .Configure::read('App.servicesImageFolder'),Configure::read('App.servicesImageWidth'),Configure::read('App.servicesImageHeight') );
          $data["Service"]["image_path"] = Configure::read('App.servicesImageFolder') . '/' . $imageUploaded;
        } else {
          if($action==='add') {
            $data["Service"]["image_path"] = null;
          } else {
            $data["Service"]["image_path"] = isset($data["Service"]["_image_path"]) ? $data["Service"]["_image_path"] : null;
          }
        }
        if ($this->Service->save($data, false)) {
          $this->Session->setFlash('The record was successfully saved.');
          $this->redirect(array('action' => 'index'));
        }
      } else {
        if($action!=='add') {
          //getting previous image
          $data['Service']['image_path']=$data["Service"]["_image_path"];
          $data['Service']['id']=$id;
        }
        $this->request->data['Service'] = $data['Service'];
        $this->set('errors', $this->Service->validationErrors);
      }
      if($action==='add') {
        $this->render('admin_add');
      } else {
        $this->render('admin_edit');
      }
  }

  //admin
  function admin_delete($id) {
    $this->layout="admin";
    $this->admin_deleteImage($id, false);
    if ($this->Service->delete($id)) {
      $this->Session->setFlash('The record has been deleted.');
      $this->redirect(array('action' => 'index'));
    }
  }

  //admin
  function admin_deleteImage($id, $redirect=true) {
    $this->layout="admin";
    $image = $this->Service->read('image_path', $id);
    $imagePath = $image['Service']['image_path'];
    if (!empty($imagePath)) {
      $result = $this->Util->deleteFile(WWW_ROOT . '/' . Configure::read('App.imageFolder') . '/' . $imagePath);
      $this->Service->save(array('Service' => array('id' => $id, 'image_path' => '')), false, array('image_path'));
      if($redirect){
        $this->Session->setFlash('The image has been deleted.');
        $this->redirect(array('action' => 'edit', $id));
      }
    }
  }
}

?>
